<?php

$dbconnect = DBconnect::getInstance();
$arrayInfo = $dbconnect->getAllPlayersAndTeams();
$json = json_encode($arrayInfo, JSON_PRETTY_PRINT);
if(isset($_POST['submit'])){
	$query = $dbconnect->getTeamInfo($_POST['tags']);
	if(mysql_num_rows($query) > 0){
		$team_no = mysql_result($query, 0, "rteam_no");
		$team = mysql_result($query, 0, "team_name");
		
		echo "<table id='teamTable'>";
		
		echo "<th colspan='2' id='teamName'>$team</th><th id='teamImg'><img src='../images/teamlogos/$team.png'></th>
				<tr><td id='teamPlayerName'>Player Name</td><td id='teamPlayerPosition'>Position</td><td id='teamPlayerPrice'>Price (m)</td></tr>";
		
		$query1 = $dbconnect->getPlayersFromTeam($team_no);
		while($players = mysql_fetch_assoc($query1)){
			$name = $players['player_name'];
			$value = $players['value'];
			$position = $players['position_id'];
			$position_name = $dbconnect->getPositionName($position);
			echo "<tr><td id='teamPlayers'>$name</td><td id='teamPositions'>$position_name</td><td id='teamPlayersValue'>$value $</td></tr>";
			
		}
		echo "</table>";
	}			
	else{
		$query = $dbconnect->getPlayerInfo($_POST['tags']);
		if(mysql_num_rows($query) > 0){
			$name = mysql_result($query, 0, "player_name");
			$value = mysql_result($query, 0, "value");
			$team = mysql_result($query, 0, "team_name");
			$totalScore = mysql_result($query, 0, "total_score");
			$position = mysql_result($query, 0, "position_id");
			$position_name = $dbconnect->getPositionName($position);
			
			echo "<table id='playerTable'>";
			
			echo "<tr><th rowspan='3' id='playerImg'><img  src='../images/realplayers/$name.jpg'></th>
					  <th id='thPlayerTeam'>Team</th><th id='thPlayerPosition'>Position</th><th id='thPlayerName'>Name</th>
				 	  <th id='thPlayerValue'>Value</th><th id='thPlayerScore'>Score</th></tr>";
			
			echo "<tr><td id='arrow'>&#8659;</td><td id='arrow'>&#8659;</td><td id='arrow'>&#8659;</td>
						<td id='arrow'>&#8659;</td><td id='arrow'>&#8659;</td></tr>";
			
			echo "<tr><td id='playerTeam'>$team</td><td id='playerPosition'>$position_name</td><td id='playerName'>$name</td>
						<td id='playerValue'>$value $</td><td id='playerScore'>$totalScore </td></tr></table>";
			
		}
		else{
			echo "<p><b>They aren't any matches!</b></p>";
		}
	}
}
?>
